<template>
  <div class="video-list-container">
    <div class="video-list-title" v-show="showTitle">
      <span>{{ title }}</span>
    </div>
    <el-row
      class="partRow"
      :gutter="20"
      v-show="isPart"
      type="flex"
      justify="space-around"
    >
      <el-col
        v-for="(videoItem, index) in videoData"
        :key="videoItem.id"
        :xl="6"
        :sm="8"
        :md="8"
        :lg="8"
        :offset="0"
        :class="showInLg(index)"
      >
        <el-card
          class="partCard"
          shadow="hover"
          @click.native="toVideo(videoItem.id)"
          :body-style="bodyPartStyle"
        >
          <el-image class="img" :src="videoItem.image" fit="cover"></el-image>
          <div class="vlogger">
            <span>{{ videoItem.title }}</span>
            <div class="info">
              <span>{{ videoItem.author }}</span>
              <span style="float: right">{{ videoItem.date }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row
      class="toalRow"
      :gutter="20"
      type="flex"
      justify="space-around"
      v-show="isTotality"
    >
      <el-col
        v-for="(videoItem, index) in videoData"
        :key="videoItem.id"
        :class="showInLg(index)"
        :xl="4"
        :sm="6"
        :md="6"
        :lg="6"
        :offset="0"
      >
        <el-card
          class="partCard"
          shadow="hover"
          @click.native="toVideo(videoItem.id)"
          :body-style="bodyPartStyle"
        >
          <el-image class="img" :src="videoItem.image" fit="cover"></el-image>
          <div class="vlogger">
            <span>{{ videoItem.title }}</span>
            <div class="info">
              <span>{{ videoItem.author }}</span>
              <span style="float: right">{{ videoItem.date }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRandomVideo } from "../api/videoApi";
export default {
  props: {
    listSize: {
      type: Number,
      default() {
        return 4;
      },
    },
    showTitle: {
      type: Boolean,
      default() {
        return false;
      },
    },
    isPart: {
      type: Boolean,
      default() {
        return false;
      },
    },
    isTotality: {
      type: Boolean,
      default() {
        return false;
      },
    },
    title: {
      type: String,
      default() {
        return "为您推荐";
      },
    },
  },
  data() {
    return {
      bodyPartStyle: {
        padding: "0px",
      },
      videoData: [],
    };
  },
  computed: {
    // 响应式处理每行的视频数量
    showInLg() {
      return function (index) {
        return { "hidden-lg-and-down": index === this.listSize - 1 };
      };
    },
  },
  methods: {
    toVideo(id) {
      this.$router.push({
        name: "video",
        query: {
          id: id,
        },
      });
    },
    getRandomVideo() {
      getRandomVideo(this.listSize)
        .then((res) => {
          this.videoData = res.data.data.rows;
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
  mounted() {
    this.getRandomVideo();
  },
};
</script>

<style lang="scss" scoped>
.video-list-container {
  width: 100%;
}

.video-list-title {
  padding-left: 25px;
  margin: 10px 0 10px 0;
  font-size: 20px;
}

.partRow {
  width: 100%;
  height: 240px;
}

.partCard {
  width: 300px;
  height: 245px;
}

.toalRow {
  width: 100%;
  height: 200px;
}

.toalCard {
  width: 280px;
  height: 200px;
}

.video-list-container span:hover {
  color: #00a1d6;
  cursor: pointer;
}

.vlogger {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.img {
  width: 100%;
  height: 190px;
}
</style>
